package best.driver.android.Utils;

import android.util.Log;
import best.driver.android.AppConfig;
import best.driver.android.AppInfo;

import static android.util.Log.*;
import static android.util.Log.ASSERT;
import static android.util.Log.ERROR;

public class Logger {
    private static final String ALL_TAG = "best";
    private final String tag;
    private final int logLevel;
    private static boolean isLoggerEnable = true;
    static {
        initLoger();
    }

    public Logger(String tag, int logLevel) {
        this.tag = tag;
        this.logLevel = logLevel;
    }

    @SuppressWarnings("rawtypes")
    public Logger(Class clazz, int logLevel) {
        this(clazz.getName(), logLevel);
    }

    public Logger(String tag) {
        this(tag, DEBUG);
    }

    @SuppressWarnings("rawtypes")
    public Logger(Class clazz) {
        this(clazz.getName(), DEBUG);
    }

    public static boolean isLoggerEnable() {
        return isLoggerEnable;
    }

    public static void setLoggerEnable(boolean loggerEnable) {
        isLoggerEnable = loggerEnable;
    }

    private static  void initLoger(){
        if (AppInfo.currentContextMode == AppInfo.ContextMode.ONLINE){
            isLoggerEnable = false;
        }else {
            isLoggerEnable = true;
        }
    }


    public void v(String format, Object... args) {
        if (isLoggable(VERBOSE)) {
            Log.v(tag, String.format(format, args));
            Log.v(ALL_TAG, tag + "," + String.format(format, args));
            LoggerSave.vDetail(tag, "%s", String.format(format, args));
            LoggerSave.v(tag+ " %s", String.format(format, args));
        }
    }

    public void d(String message) {
        if (isLoggable(DEBUG)) {
            Log.d(tag, message);
            Log.d(ALL_TAG, tag + "," + message);
            LoggerSave.vDetail(tag, "%s", message);
            LoggerSave.v(tag+ " %s", message);
        }
    }

    public void i(String format, Object... args) {
        if (isLoggable(INFO)) {
            Log.i(tag, String.format(format, args));
            Log.i(ALL_TAG, tag + "," + String.format(format, args));
            LoggerSave.vDetail(tag, "%s", String.format(format, args));
            LoggerSave.v(tag+ " %s", String.format(format, args));
        }
    }

    public void w(String format, Object... args) {
        if (isLoggable(WARN)) {
            Log.w(tag, String.format(format, args));
            Log.w(ALL_TAG, tag + "," + String.format(format, args));
            LoggerSave.vDetail(tag, "%s", String.format(format, args));
            LoggerSave.v(tag+ " %s", String.format(format, args));
        }
    }

    public void e(String format, Object... args) {
        if (isLoggable(ERROR)) {
            Log.e(tag, String.format(format, args));
            Log.e(ALL_TAG, tag + "," + String.format(format, args));
            LoggerSave.vDetail(tag, "%s", String.format(format, args));
            LoggerSave.v(tag+ " %s", String.format(format, args));
        }
    }

    public void wtf(String format, Object... args) {
        if (isLoggable(ASSERT)) {
            Log.wtf(tag, String.format(format, args));
            Log.wtf(ALL_TAG, tag + "," + String.format(format, args));
            LoggerSave.vDetail(tag, "%s", String.format(format, args));
            LoggerSave.v(tag + " %s", String.format(format, args));
        }
    }

    private boolean isLoggable(int level) {
        return isLoggerEnable && level >= logLevel;
    }

    public static Logger createLogger(String className) {
        return new Logger(className);
    }

    public static void d(String tag, String msg) {
        if (Logger.isLoggerEnable()) {
            Log.d(tag, msg);
        }
    }
}